//package com.zck.aiagent.advisor;
//
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.ai.chat.client.advisor.api.*;
//import org.springframework.ai.chat.messages.AssistantMessage;
//import org.springframework.ai.chat.metadata.ChatResponseMetadata;
//import org.springframework.ai.chat.model.ChatResponse;
//import org.springframework.ai.chat.model.Generation;
//import org.springframework.stereotype.Component;
//import reactor.core.publisher.Flux;
//
//import java.util.Collections;
//import java.util.List;
//import java.util.Map;
//
///**
// * 违禁词校验Advisor
// */
//@Component
//@Slf4j
//public class MyViolationProhibitionAdvisor implements CallAroundAdvisor , StreamAroundAdvisor {
//
//    /**
//     * 校验是否包含违禁词
//     * @return true-通过校验 false-包含违禁词
//     */
//    private boolean validateText(String text) {
//        String[] split = text.split(",");
//        for (String s : split) {
//            s = s.trim();
//            if (s.contains("中国")) {  // 改为contains更准确
//                return false;
//            }
//        }
//        return true;
//    }
//
//    @Override
//    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
//        if (validateText(advisedRequest.userText())) {
//            return chain.nextAroundCall(advisedRequest);
//        }else{
//            log.info("AI Promrt里面有违禁词,请重新输入!");
//            // 构建完整的拒绝响应
//            return AdvisedResponse.builder()
//                    .response(createRejectionResponse())  // 关键：必须设置ChatResponse
//                    .adviseContext(Collections.emptyMap())
//                    .build();
//        }
//
//    }
//
//    @Override
//    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
//        return null;
//    }
//
//    @Override
//    public String getName() {
//        return this.getClass().getSimpleName();
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//
//    /**
//     * 创建阻断响应（完全终止处理链）
//     */
//    private AdvisedResponse createBlockedResponse(AdvisedRequest request) {
//        return AdvisedResponse.builder()
//                .response(new ChatResponse(
//                        List.of(new Generation(
//                                new AssistantMessage("请求因包含违禁词已被拦截")
//                        )),
//                        new ChatResponseMetadata()
//                ))
//                .adviseContext(Map.of(
//                        "blocked", true,          // 标记为阻断
//                        "violationType", "BANNED_WORD"
//                ))
//                .chatRequest(request.chatRequest())
//                .blockFurtherProcessing(true) // 关键：阻断后续处理
//                .build();
//    }
//
//    private ChatClientResponse createFailureResponse(ChatClientRequest chatClientRequest) {
//        return ChatClientResponse.builder().chatResponse(ChatResponse.builder().generations(List.of(new Generation(new AssistantMessage(this.failureResponse)))).build()).context(Map.copyOf(chatClientRequest.context())).build();
//    }
//}
